﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BadHabit.Factory.Windows {
    /// <summary>
    /// Interaction logic for TaskDetail.xaml
    /// </summary>
    public partial class TaskDetail :  BadHabit.WPFTools.ShapedWindow {


        BindingListCollectionView collectionView;
        

        private Data.ProjectDataSet.ProjectRow Project { get; set; }

        
        private Data.TaskDataSet.TaskRow TaskRow {
            get {
                return Helper.GetRowFromRowView<Data.TaskDataSet.TaskRow>(DataContext);
            }
        }

        /// <summary>
        /// Constructor for a collection view detail.
        /// </summary>
        /// <param name="project"></param>
        /// <param name="collectionView"></param>
        public TaskDetail(Data.ProjectDataSet.ProjectRow project, BindingListCollectionView collectionView) {
            InitializeComponent();

            this.collectionView = collectionView;
            DataContext = collectionView.CurrentItem;
            Project = project;


            artifactListBox.Project = Project;
            artifactListBox.BacklogItemUId = TaskRow.BacklogItemUId;

            projectNameTextBlock.Text = Project.ClientName + " - " + Project.ProjectName;


            SetProjectMemberList(Client.ProjectMemberList);
            SetUnitList(Client.UnitList);

            commitButton.Visibility = Visibility.Collapsed;

        }


        /// <summary>
        /// Constructor for a single task.
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="projectUId"></param>
        public TaskDetail(int taskId) {
            InitializeComponent();

            Data.TaskDataSet.TaskRow taskRow = Client.RetriveTask(taskId);
            if (taskRow == null)
                return;
            DataContext = taskRow.Table.DefaultView[taskRow.Table.Rows.IndexOf(taskRow)];
            
            Data.ProjectDataSet dsProject = Client.RetriveProjectAndRolesOnly(taskRow.ProjectUId);
            if (dsProject == null || dsProject.Project.Count == 0)
                return;

            Project = dsProject.Project[0];                          
            artifactListBox.Project = Project;
            artifactListBox.BacklogItemUId = TaskRow.BacklogItemUId;

            projectNameTextBlock.Text = Project.ClientName + " - " + Project.ProjectName;

            SetUnitList(dsProject.BacklogItemUnit.DefaultView);

            // only team members can work
            if (Project.AccessMode == (short)ProjectAccessMode.ALL_SEE_TEAM_WORK)
                SetProjectMemberList(Client.GetOnlyRoleMembers(dsProject, true));
            else
                SetProjectMemberList(Client.GetOnlyRoleMembers(dsProject, false));


        }

        private void SetProjectMemberList(System.Data.DataView view) {
            ObjectDataProvider ob = Resources["ProjectMemberList"] as ObjectDataProvider;
            ob.ObjectInstance = view;
        }

        private void SetUnitList(System.Data.DataView view) {
            ObjectDataProvider ob = Resources["UnitList"] as ObjectDataProvider;
            ob.ObjectInstance = view;
        }


        /// <summary>
        /// Filters the project team members according the Project Access Mode.
        /// </summary>
        private void UpdateProjectMemberList(Data.ProjectDataSet dsProject) {
            if (dsProject == null)
                return;

            if (dsProject.Project.Count == 0)
                return;

            ObjectDataProvider ob = Resources["ProjectMemberList"] as ObjectDataProvider;
            
            // only team members can work
            if (dsProject.Project[0].AccessMode == (short)ProjectAccessMode.ALL_SEE_TEAM_WORK)
                ob.ObjectInstance = Client.GetOnlyRoleMembers(dsProject, true);
            else
                ob.ObjectInstance = Client.GetOnlyRoleMembers(dsProject, false);
        }
        


 
        private void ShapedWindow_Loaded(object sender, RoutedEventArgs e) {
            artifactListBox.LoadArtifacts();
        }

        private void ProjectFolder_Click(object sender, RoutedEventArgs e) {
            Helper.OpenOSWindow(Project.ProjectDocBase);
        }

        private void AddLink_Click(object sender, RoutedEventArgs e) {
            tailTabItem.IsSelected = true;
            BadHabit.Factory.Controls.TaskViewerBase.AddTaskMaterial(TaskRow.TaskUId, Project.ProjectDocBase, true);
            artifactListBox.LoadArtifacts();
        }

        private void AddFile_Click(object sender, RoutedEventArgs e) {
            tailTabItem.IsSelected = true;
            BadHabit.Factory.Controls.TaskViewerBase.AddTaskMaterial(TaskRow.TaskUId, Project.ProjectDocBase, false);
            artifactListBox.LoadArtifacts();
        }

        
        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (TaskRow == null)
                return;
            e.CanExecute = TaskRow.Table.DataSet.HasChanges();
            e.Handled = true;
        }

        private void Save_Executed(object sender, ExecutedRoutedEventArgs e) {
            if(Client.SaveTasks(TaskRow.Table as Data.TaskDataSet.TaskDataTable))
                Close();
        }


        
    }
}
